import { defineStore } from "pinia";
import { ref } from "vue";
import { museumApi, ticketApi } from "../api";

export const useMuseumStore = defineStore("museum", () => {
  // 状态
  const museums = ref([]);
  const tickets = ref({}); // 按博物馆ID分组的票种
  const loading = ref(false);

  // 方法
  const fetchMuseums = async () => {
    try {
      loading.value = true;
      const response = await museumApi.getMuseums();
      museums.value = response.data;
    } catch (error) {
      console.error("获取博物馆列表失败:", error);
      throw error;
    } finally {
      loading.value = false;
    }
  };

  const addMuseum = async (museum) => {
    try {
      loading.value = true;
      const response = await museumApi.addMuseum(museum);
      museums.value.push(response.data);
    } catch (error) {
      console.error("添加博物馆失败:", error);
      throw error;
    } finally {
      loading.value = false;
    }
  };

  const updateMuseum = async (id, museum) => {
    try {
      loading.value = true;
      const response = await museumApi.updateMuseum(id, museum);
      const index = museums.value.findIndex((item) => item.id === id);
      if (index > -1) {
        museums.value[index] = response.data;
      }
    } catch (error) {
      console.error("更新博物馆失败:", error);
      throw error;
    } finally {
      loading.value = false;
    }
  };

  const deleteMuseum = async (id) => {
    try {
      loading.value = true;
      await museumApi.deleteMuseum(id);
      const index = museums.value.findIndex((item) => item.id === id);
      if (index > -1) {
        museums.value.splice(index, 1);
      }
      // 删除相关票种
      delete tickets.value[id];
    } catch (error) {
      console.error("删除博物馆失败:", error);
      throw error;
    } finally {
      loading.value = false;
    }
  };

  const fetchTickets = async (museumId) => {
    try {
      loading.value = true;
      const response = await ticketApi.getTicketTypes(museumId);
      tickets.value[museumId] = response.data;
    } catch (error) {
      console.error("获取票种列表失败:", error);
      throw error;
    } finally {
      loading.value = false;
    }
  };

  const addTicket = async (museumId, ticket) => {
    try {
      loading.value = true;
      const response = await ticketApi.addTicketType(museumId, ticket);
      if (!tickets.value[museumId]) {
        tickets.value[museumId] = [];
      }
      tickets.value[museumId].push(response.data);
    } catch (error) {
      console.error("添加票种失败:", error);
      throw error;
    } finally {
      loading.value = false;
    }
  };

  const updateTicket = async (museumId, ticketId, ticket) => {
    try {
      loading.value = true;
      const response = await ticketApi.updateTicketType(
        museumId,
        ticketId,
        ticket
      );
      const museumTickets = tickets.value[museumId];
      if (museumTickets) {
        const index = museumTickets.findIndex((item) => item.id === ticketId);
        if (index > -1) {
          museumTickets[index] = response.data;
        }
      }
    } catch (error) {
      console.error("更新票种失败:", error);
      throw error;
    } finally {
      loading.value = false;
    }
  };

  const deleteTicket = async (museumId, ticketId) => {
    try {
      loading.value = true;
      await ticketApi.deleteTicketType(museumId, ticketId);
      const museumTickets = tickets.value[museumId];
      if (museumTickets) {
        const index = museumTickets.findIndex((item) => item.id === ticketId);
        if (index > -1) {
          museumTickets.splice(index, 1);
        }
      }
    } catch (error) {
      console.error("删除票种失败:", error);
      throw error;
    } finally {
      loading.value = false;
    }
  };

  return {
    // 状态
    museums,
    tickets,
    loading,

    // 方法
    fetchMuseums,
    addMuseum,
    updateMuseum,
    deleteMuseum,

    fetchTickets,
    addTicket,
    updateTicket,
    deleteTicket,
  };
});
